package com.upup.train_management.config;

import com.upup.train_management.entity.Station;
import com.upup.train_management.entity.Train;
import com.upup.train_management.entity.User;
import com.upup.train_management.repository.StationRepository;
import com.upup.train_management.repository.TrainRepository;
import com.upup.train_management.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class DataInitializer implements CommandLineRunner {

    private final UserRepository userRepository;
    private final StationRepository stationRepository;
    private final TrainRepository trainRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    public DataInitializer(UserRepository userRepository, StationRepository stationRepository, TrainRepository trainRepository) {
        this.userRepository = userRepository;
        this.stationRepository = stationRepository;
        this.trainRepository = trainRepository;
    }

    @Override
    public void run(String... args) {
        // 初始化管理员用户
        if (userRepository.findByUsername("admin") == null) {
            User admin = new User();
            admin.setUsername("admin");
            admin.setPassword(passwordEncoder.encode("admin123"));
            admin.setRealName("系统管理员");
            admin.setEmail("admin@train.com");
            admin.setPhone("13800138000");
            admin.setIdCard("110101199001011234");
            admin.setUserRole("ADMIN");
            admin.setCreatedTime(LocalDateTime.now());
            userRepository.save(admin);
            System.out.println("已创建默认管理员用户: admin/admin123");
        }

        // 初始化普通用户
        if (userRepository.findByUsername("user") == null) {
            User user = new User();
            user.setUsername("user");
            user.setPassword(passwordEncoder.encode("user123"));
            user.setRealName("普通用户");
            user.setEmail("user@train.com");
            user.setPhone("13800138001");
            user.setIdCard("110101199001011235");
            user.setUserRole("USER");
            user.setCreatedTime(LocalDateTime.now());
            userRepository.save(user);
            System.out.println("已创建普通用户: user/user123");
        }

        // 初始化一些常用车站
        if (stationRepository.count() == 0) {
            String[][] stationData = {
                    {"北京站", "BJP", "北京市东城区毛家湾胡同甲13号", "北京", "北京"},
                    {"上海站", "SHH", "上海市静安区秣陵路303号", "上海", "上海"},
                    {"广州站", "GZQ", "广州市越秀区环市西路159号", "广州", "广东"},
                    {"深圳站", "SZQ", "深圳市罗湖区建设路2017号", "深圳", "广东"},
                    {"杭州站", "HZH", "杭州市上城区江城路1号", "杭州", "浙江"},
                    {"南京站", "NJH", "南京市玄武区龙蟠路1号", "南京", "江苏"},
                    {"武汉站", "WHN", "武汉市江岸区解放大道1021号", "武汉", "湖北"},
                    {"成都站", "CDW", "成都市金牛区北站西路1号", "成都", "四川"},
                    {"西安站", "XAY", "西安市新城区环城北路", "西安", "陕西"},
                    {"重庆站", "CQW", "重庆市渝中区双钢路1号", "重庆", "重庆"}
            };

            for (String[] data : stationData) {
                Station station = new Station();
                station.setStationName(data[0]);
                station.setStationCode(data[1]);
                station.setAddress(data[2]);
                station.setCityName(data[3]);
                station.setProvinceName(data[4]);
                stationRepository.save(station);
            }
        }

        // 初始化一些列车
        if (trainRepository.count() == 0) {
            String[][] trainData = {
                    {"G1", "高铁", "16", "80", "576"},
                    {"G2", "高铁", "16", "80", "576"},
                    {"K1001", "快速", "18", "60", "720"},
                    {"K1002", "快速", "18", "60", "720"},
                    {"D301", "动车", "16", "70", "650"},
                    {"D302", "动车", "16", "70", "650"},
                    {"Z21", "直达", "20", "50", "800"},
                    {"Z22", "直达", "20", "50", "800"},
                    {"T109", "特快", "20", "48", "760"},
                    {"T110", "特快", "20", "48", "760"}
            };

            for (String[] data : trainData) {
                Train train = new Train();
                train.setTrainNumber(data[0]);
                train.setTrainType(data[1]);
                train.setFirstClassSeats(Integer.parseInt(data[2]));
                train.setSecondClassSeats(Integer.parseInt(data[3]));
                train.setTotalSeats(Integer.parseInt(data[4]));
                trainRepository.save(train);
            }
        }
    }
}